/**
 * 分页混入
 */
import _ from 'lodash'
import Fuse from 'fuse.js'
const settings = {
	loading:false,
	queryStream:'',
	fuse: null,
	currentPage: 1,
	pageSizes: [5, 10, 15, 20],
	pageSize: 10,
	pageData:[]
}
const createFuse = (keys,context) => context.fuse = new Fuse(context.pageData, {
	shouldSort: true,
	threshold: 0.4,
	location: 0,
	distance: 100,
	maxPatternLength: 32,
	minMatchCharLength: 1,
	keys,
})
const pagination = {
	data() {
		return {
			copyPageData: [],
			...settings
		}
	},
	computed: {
		total() {
			return this.pageData.length;
		},
		list() {
			return this.pageData.slice().splice((this.currentPage - 1) * this.pageSize, this.pageSize)
		}
	},
	watch: {
		queryStream(n, o) {
			this.querySearch(n);
		}
	},
	created() {
		this.handleReset()
	},
	methods: {
		// 每页显示多少条
		handleSizeChange(val) {
			this.pageSize = val;
		},
		// 当前第几页
		handleCurrentChange(val) {
			this.currentPage = val;
		},
		querySearch(query) {
			if (query !== '') {
				this.pageData = this.fuse.search(query);
			} else {
				this.pageData = this.copyPageData;
			}
		},
		handleReset() {
			this.copyPageData = _.cloneDeep(this.pageData);
		},
		createFuse,
	}
}
const backendPaging = {
	data() {
		return {
			total:0,
			...settings
		}
	},
	created() {
		this.fetchList()
	},
	methods: {
		handleSizeChange(val) {
			this.pageSize = val
			this.fetchList()
		},
		handleCurrentChange(val) {
			this.currentPage = val
			this.fetchList()
		},
		// createFuse,
		querySearch() {
			this.fetchList()
		},
		fetchList(){}
	}
}
export { pagination, backendPaging }
export default pagination;
